package net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TcpClientTest1 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);

        // 发送输出流到服务端
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("YUYUDEQIU".getBytes(StandardCharsets.UTF_8));

        socket.shutdownOutput();

        // 接收服务端发送的数据流
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = inputStream.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }

        System.out.println("Server message: " + sb.toString());

        socket.shutdownInput();

        socket.close();

    }
}
